<template>
  <!-- 员工管理页 -->
  <div>
    <el-form :inline="true" :model="params" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" @click="toAdd">添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.username"
          placeholder="请输入用户名"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.status" placeholder="请选择状态" clearable>
          <el-option label="正常" value="正常" />
          <el-option label="禁用" value="禁用" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="waiters" style="width: 100%">
      <el-table-column prop="id" label="编号" width="180" />
      <el-table-column
        prop="username"
        label="用户名"
        width="180"
      />
      <el-table-column prop="realname" label="姓名" />
      <el-table-column prop="telephone" label="手机号" />
      <el-table-column prop="gender" label="性别" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '正常'"
            type="primary"
            plain
            disabled
          >正常</el-button>
          <el-button v-else type="danger" plain disabled>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <a href="#" @click="toEdit(scope.row)" style="color:cadetblue;margin-right:10px">编辑</a>
          <a href="#" @click="toDelete(scope.row)" style="color:red;margin-right:10px">删除</a>
          <a href="#" @click="toDetail(scope.row.id)" style="color:blue">详情</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[3, 5, 8, 10, 15, 20, 25, 30]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="params.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 保存或更新 -->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form ref="myForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="telephone">
          <el-input
            v-model="form.telephone"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password" placeholder="请输入手机号">
          <el-input
            v-model="form.password"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="男" />
            <el-radio label="女" />
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if=" this.title == '编辑员工信息'"
          label="状态"
          prop="status"
        >
          <el-radio-group v-model="form.status">
            <el-radio label="正常" />
            <el-radio label="禁用" />
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="goSure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { allUser, changeWaiter, delWaiter, updateWaiter } from '@/api/waiter'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        roleId: 1
      },
      total: '',
      waiters: [],
      title: '',
      visible: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        status: [{ required: true, trigger: 'blur' }]
      },
      form: {},
      visibleForm: true
    }
  },
  computed: {},
  created() {
    this.getWaiters()
  },
  mounted() {},
  methods: {
    // 查询所有员工信息
    async getWaiters() {
      const res = await allUser(this.params)
      this.waiters = res.data.list
      this.total = res.data.total
    },
    // 当前页面条数发生改变时触发的方法
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.params.page = 1
      this.getWaiters()
    },
    // 当前页码改变触发的方法
    handleCurrentChange(page) {
      this.params.page = page
      this.getWaiters()
    },
    // 查询按钮
    toSearch() {
      this.params.page = 1;
      this.getWaiters();
    },
    // 添加按钮
    toAdd() {
      this.title = '添加员工信息'
      this.visible = true
      this.form = {}
    },
    // 确定按钮
    goSure() {
      this.$set(this.form, 'roleId', 1)
      if (this.title == '添加员工信息') {
        this.$refs.myForm.validate(async(valid) => {
          if (valid) {
            const res = await changeWaiter(this.form)
            this.$notify({
              type: 'success',
              message: res.message
            })
            this.visible = false   
            this.getWaiters()      
          } else {
            return false
          }
        })
      }else {
        this.$refs.myForm.validate(async(valid) => {         
          if (valid) {
            const res = await updateWaiter(this.form)
            console.log(this.title);
            this.$notify({
              type: 'success',
              message: res.message
            })
            this.visible = false
            this.getWaiters()
          } else {
            return false
          }
        })
      }
    },
    toEdit(row) {
      this.title = '编辑员工信息'
      this.visible = true
      this.form = { ...row }
    },
    async toDelete(row) {
      await delWaiter(row)
      this.getWaiters()
    },
    toDetail(id) {
      this.$router.push({
        path: 'Details',
        query: { id }
      })
    }
  }
}
</script>
<style scoped>
</style>
